﻿using AutoMapper;
using DFGK_WMS.Context;
using DFGK_WMS.IRepository;
using DFGK_WMS.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK_WMS.Repository
{
    public class TStocktakeResultRecordRepository : BaseRepository<TStocktakeResultRecord>, ITStocktakeResultRecordRepository
    {
        public TStocktakeResultRecordRepository(DFGK_DB context, IMapper mapper) : base(context, mapper)
        {
        }

        /// <summary>
        /// 盘点库显示
        /// </summary>
        /// <param name="wid"></param>
        /// <param name="aid"></param>
        /// <param name="status"></param>
        /// <param name="isHandle"></param>
        /// <param name="orderSN"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public List<TStocktakeResultRecord> GetTStocktakeResultRecordList(int wid, int aid, int status, int isHandle, string orderSN, int pageIndex, int pageSize, out int totalCount)
        {
            var list = Context.TStocktakeResultRecords.AsQueryable();

            //库房
            if(wid!=0)
            {
                list = list.Where(x => x.WarehouseId == wid);
            }
            //库区
            if (aid != 0)
            {
                list = list.Where(x => x.AreaId == aid);
            }
            //状态
            if(status!=0)
            {
                list = list.Where(x => x.Status == status);
            }
            //是否已处理
            if(isHandle<2)
            {
                list = list.Where(x => x.IsHandle == isHandle);
            }
            if(!string.IsNullOrEmpty(orderSN))
            {
                list = list.Where(x => x.OrderSn.Contains(orderSN));
            }
            totalCount = list.Count();

            list = list.OrderByDescending(x => x.CreateDate).Skip((pageIndex - 1) * pageSize).Take(pageSize);
            return list.ToList();
        }
    }
}
